/*******************************************************************
*
*    FILE: robot_interrupts.c
*    Description: 
*    AUTHOR: Tim Cook
*    DATE:9/20/2009
*
*******************************************************************/

/** include files **/
#include <p18f4520.h>
#include "servo.h"
#include "motor.h"

void InterruptHandlerHigh ();

/** external data **/
extern unsigned short int servo_position;

/** public functions **/
#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh (void) {
    _asm
        goto InterruptHandlerHigh //jump to interrupt routine
     _endasm
}

#pragma code
#pragma interrupt InterruptHandlerHigh

void InterruptHandlerHigh () {
    if (TMR0_INT_FLAG) {            //check for TMR0 overflow
        INTCONbits.TMR0IF = 0;      //clear interrupt flag for TMR1
        write_timer0( TMR0_VALUE );   //write the value to TMR0
            //required to generate 50 Hz interrrupts that is defined
            //in servo.h

        //write direction to turn and
        //enable TMR1 interrupts
        write_timer1( servo_position );
        PIE1 = PIE1 | 0x01;

        //write to servo
        PORTBbits.RB1 = 1;     //write a 1 to servo port

        //enable TMR1 peripheral
        //T1CON = T1CON | 0x01; 
        
    } else if ( TMR1_INT_FLAG ){
        PIR1 = PIR1 & 0xFE; //clear TMR1 interrupt flag
        PIE1 = PIE1 & 0xFE; //disable TMR1 interrupts
        //T1CON = T1CON & (~0x01); //disable T1

        //clear servo pin for low part of cycle
        PORTBbits.RB1 = 0;
    }
    INTCONbits.GIEH = 1; //renable global interrupts
}
//rtfm_bdctfm


